function [ arrChar, arrCharS ] = getCharacter( img, imgS, IS_DEBUG)
%GETCHARACTER Summary of this function goes here
%   Detailed explanation goes here

numChar=0;
arrCharTemp={};
arrCharTempS={};
location = {};
if IS_DEBUG
    figure();
    imshow(img);
    
end


ccw =  bwconncomp(img, 8);
featureData = regionprops(ccw,'BoundingBox','Area','Orientation');


for j= 1:ccw.NumObjects
    width = featureData(j).BoundingBox(3);
    height = featureData(j).BoundingBox(4);
    area = featureData(j).BoundingBox(3)*featureData(j).BoundingBox(4);
    numPixel = length(ccw.PixelIdxList{j});
    apecArea = numPixel/area;
    
   if height>32 && width > 4 && numPixel >40 && apecArea > 0.2
        grain = false(size(img));
        grain(ccw.PixelIdxList{j}) = true;
        tempsubimg = imcrop(grain, featureData(j).BoundingBox);
        tempim = bwareaopen(tempsubimg,100);
        numChar = numChar +1;
        
        location{numChar} = [featureData(j).BoundingBox(1) featureData(j).BoundingBox(2)];
        arrCharTemp{numChar} = tempim;
        arrCharTempS{numChar} = imcrop(imgS,featureData(j).BoundingBox);
        
    end
end


%[x y w h]

arrChar = {};
arrCharS = {};
h = size(img,1);
sumloc = cellfun(@(x) x(1)+ floor((x(2)/(h*0.4)))*200, location);
sumlocsort = sort(sumloc);
for i=1:numChar
    idx = find(sumloc==sumlocsort(i),1,'first');
    arrChar{i} = arrCharTemp{idx};
    arrCharS{i} = arrCharTempS{idx};
end
end

